﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Business;

public partial class client_ManageCategory : @base
{
    DataTable categories = new DataTable();
    ReportServices rs = new ReportServices();
    DataSheetService ds = new DataSheetService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (UserDetails == null)
        {
            string message = "User has been logged out!";
            Server.Transfer("ErrorMessage.aspx?message=" + message);
        }
        if (!IsPostBack)
        {
            categories = ds.getCategory();
            foreach (DataRow r in categories.Rows)
            {
                string category_id = ( r["category_id"].ToString());
                string categoryName =r["category_Name"].ToString();

                System.Web.UI.WebControls.ListItem ls = new System.Web.UI.WebControls.ListItem(categoryName, category_id, true);
                DropDownClientList.Items.Add(ls);
                Session["TaskTable"] = getTable();

                //Bind data to the GridView control.
                BindData();
            }
        }
        else
        {
            Session["TaskTable"] = setSession();

        }

    }
    protected DataTable setSession()
    {
        int tableNumber = TaskGridView.Rows.Count;
        DataTable taskTable = new DataTable("TaskList");

        // Create the columns.
        taskTable.Columns.Add("Attribute", typeof(string));
        taskTable.Columns.Add("Value", typeof(string));

        for (int i = 0; i < tableNumber; i++)
        {
            GridViewRow row = TaskGridView.Rows[i];
            string att = ((TextBox)(row.Cells[1].FindControl("TextBox1"))).Text.ToString();
            string val = ((TextBox)(row.Cells[1].FindControl("TextBox2"))).Text.ToString();
            DataRow tableRow = taskTable.NewRow();
            tableRow["Attribute"] = att;
            tableRow["Value"] = val;
            taskTable.Rows.Add(tableRow);

        }
        return taskTable;

    }
    protected DataTable getTable()
    {
        // Create a new table.
        DataTable taskTable = new DataTable("TaskList");

        // Create the columns.
        taskTable.Columns.Add("Attribute", typeof(string));
        int category_id =ToInt(DropDownClientList.SelectedValue.ToString());
        taskTable = ds.getCategoryTemplateByID(category_id);    
       
        return taskTable;
    }
    private void BindData()
    {
        TaskGridView.DataSource = Session["TaskTable"];
        TaskGridView.DataBind();
    }
    protected void Component_Changed(object sender, EventArgs e)
    {

        Session["TaskTable"] = getTable();
        BindData();
    }
    protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Set the edit index.
        TaskGridView.EditIndex = e.NewEditIndex;
        //Bind data to the GridView control.
        BindData();
    }

    protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //Reset the edit index.
        TaskGridView.EditIndex = -1;
        //Bind data to the GridView control.
        BindData();
    }
    protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Retrieve the table from the session object.
        DataTable dt = (DataTable)Session["TaskTable"];

        //Update the values.
        GridViewRow row = TaskGridView.Rows[e.RowIndex];
        dt.Rows[row.DataItemIndex]["Attribute"] = ((TextBox)(row.FindControl("TextBox1"))).Text;       
        Session["TaskTable"] = dt;
        //Reset the edit index.
        TaskGridView.EditIndex = -1;

        //Bind data to the GridView control.
        BindData();
    }
    protected void TaskGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
    {
        DataTable dt = (DataTable)Session["TaskTable"];
        int k = dt.Rows.Count;
        GridViewRow row = TaskGridView.Rows[e.RowIndex];

        string att = ((TextBox)(row.Cells[1].FindControl("TextBox1"))).Text.ToString();

        if (att.ToUpper() == "UIN")
        {
            string jscript = "<script language='javascript'>alert('Cannot delete such attribute!')</script>";
            // Page.RegisterClientScriptBlock("keyClientBlock",jscript);
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jscript);

        }
        else
        {
            // dt.Rows[row.DataItemIndex].Delete();
            dt.Rows.RemoveAt(e.RowIndex);
            k = dt.Rows.Count;
            Session["TaskTable"] = dt;
        }
    }
    protected void TaskGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int i = e.Row.DataItemIndex;
          //  e.Row.Cells[0].ForeColor = Color.Blue;
            TextBox tb1 = (TextBox)e.Row.FindControl("TextBox1");
            tb1.Text = ((DataTable)(Session["TaskTable"])).Rows[i]["Attribute"].ToString();

           
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)Session["TaskTable"];

        string category_id = DropDownClientList.SelectedValue.ToString();
        ds.updateCategoryData(dt, category_id);
        string jscript = "<script language='javascript'>alert('Edit successfully!')</script>";
        //Page.RegisterClientScriptBlock("key2ClientBlock", jscript);
        //RegisterClientScriptBlock("key2ClientBlock", jscript);
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key2ClientBlock", jscript);

    }
    /*   public void setPage(mshtml.HTMLWindow2Class JSFile1,mshtml.HTMLWindow2Class JSFile2 )
       {
           window = JSFile;
       }*/
    protected void AddAttribute(object sender, EventArgs e)
    {
        string attribute = txtAttribute.Text.ToString();
       if (attribute == "")
        {

            string jscript = "<script language='javascript'>alert('Should input an attribute')</script>";
           // Page.RegisterClientScriptBlock("keyClientBlock", jscript);
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jscript);
            // Page.RegisterStartupScript("myScript", "<script language=JavaScript>showAlert();</script>");
        }
        else
        {
            if (attribute.ToUpper() == "UIN")
            {
                string jscript = "<script language='javascript'>alert('Cannot add such attribute')</script>";
                // Page.RegisterClientScriptBlock("keyClientBlock",jscript);
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jscript);

            }
            else
            {
                DataTable dt = (DataTable)Session["TaskTable"];
                int ok = 1;
                foreach (DataRow r in dt.Rows)
                {
                    string att = r["Attribute"].ToString();
                    if (attribute == att)
                    {
                        ok = 0;
                        break;
                    }
                }
                if (ok == 0)
                {
                    string jscript = "<script language='javascript'>alert('Cannot add an old attribute again!')</script>";
                    // Page.RegisterClientScriptBlock("keyClientBlock",jscript);
                    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jscript);

                }
                else
                {
                    DataRow newAttribute = dt.NewRow();
                    newAttribute["Attribute"] = attribute;

                    dt.Rows.Add(newAttribute);
                    Session["TaskGridView"] = dt;
                    txtAttribute.Text = "";
                }
            }
        }
        BindData();

    }
}
